package com.jjkj.environmental.environmental.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.jjkj.environmental.environmental.R
import com.jjkj.environmental.environmental.adapter.MainAdapter
import com.jjkj.environmental.environmental.bean.model.CarInfo
import com.jjkj.environmental.environmental.bean.model.CheckStation
import com.jjkj.environmental.environmental.presenter.MainPresenter
import com.jjkj.environmental.environmental.ui.base.BaseActivity
import com.jjkj.environmental.environmental.utils.LitePalHelper
import com.jjkj.environmental.environmental.view.MainView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity<MainView, MainPresenter>(), MainView, View.OnClickListener {


    private lateinit var adapter: MainAdapter

    companion object {
        fun jumpTo(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initInject() {
        getComponent().inject(this)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initData()
    }

    override fun onDataLoad(bean: List<CarInfo>) {
        adapter.replaceData(bean)
        if (swipe_refresh_l.isRefreshing) {
            swipe_refresh_l.isRefreshing = false
        }
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.tv_search -> search(edt_search_key.text.toString())
        }
    }

    private fun initView() {
        rcv_car_list.layoutManager = LinearLayoutManager(this)
        adapter = MainAdapter(R.layout.main_rcv_item)
        rcv_car_list.adapter = adapter
        adapter.setOnItemClickListener { adapter, _, position ->
            CheckActivity.jumpTo(this, adapter.data[position] as CarInfo)
        }
        swipe_refresh_l.setOnRefreshListener { initData() }
    }

    private fun initData() {
        val checkStation = CheckStation(LitePalHelper.getUserSetting().station, "")
        getPresenter().loadCatList(checkStation)
    }

    private fun search(key: String) {
        if (adapter.data.size >= 0) {
            val iterator = adapter.data.iterator()
            iterator.forEach {
                if (
                        !it.czmc!!.contains(key) &&
                        !it.fdjbh!!.contains(key) &&
                        !it.cljcrlsh!!.contains(key) &&
                        !it.hphm!!.contains(key) &&
                        !it.cjh!!.contains(key)) {
                    iterator.remove()
                }
            }
        }
        adapter.notifyDataSetChanged()
    }

    override fun showException(throwable: Throwable) {
        super.showException(throwable)
        if (swipe_refresh_l.isRefreshing) {
            swipe_refresh_l.isRefreshing = false
        }
    }
}
